package com.ruoyi.activiti.bpmnparse.x6translator;

import com.ruoyi.activiti.bpmnparse.BpmnHelper;
import com.ruoyi.activiti.bpmnparse.Translator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SequenceFlowTranslator extends Translator {
    private List<Map<String,Object>> x6ListMap;
    private BpmnHelper helper = new X6Helper();
    private String type = "sequenceFlow";
    public SequenceFlowTranslator(List<Map<String,Object>> x6ListMap){
        this.x6ListMap = x6ListMap;
    }
    @Override
    public Map<String, Object> translate(Map<String, Object> sourceMap) {
        // 获取x6json中对象的data
        Map<String, Object> dataMap = (Map<String, Object>) sourceMap.get("data");
        Map<String, Object> bpmnMap = new HashMap<>();
        // 获取x6json中cell的id
        String id = (String) dataMap.get("id");
        // 获取x6json中的source和target引用的id
        Map<String,Object> sourceData = (Map<String, Object>) sourceMap.get("source");
        String sourceId = (String) sourceData.get("cell");
        Map<String,Object> targetData = (Map<String, Object>) sourceMap.get("target");
        String targetId = (String) targetData.get("cell");
        // 获取x6json中data中自定义的conditionExpression数据。
        Map<String,Object> conditionExpression = (Map<String, Object>) dataMap.get("conditionExpression");
        if(conditionExpression != null) {
            String xsiType = (String) conditionExpression.get("xsiType");
            String value = (String) conditionExpression.get("value");
            Map<String,Object> bpmnConditionExpression = new HashMap<>();
            bpmnConditionExpression.put("xsi:type",xsiType);
            bpmnConditionExpression.put("value",value);
            bpmnMap.put("conditionExpression",bpmnConditionExpression);
        }
        // 把x6json map映射转换成bpmn map
        String bpmnSourceId = helper.getBpmnKey(x6ListMap,sourceId);
        String bpmnTargetId = helper.getBpmnKey(x6ListMap,targetId);
        bpmnMap.put(Translator.BPMN_ID,id);
        bpmnMap.put(Translator.BPMN_TYPE,type);
        bpmnMap.put("sourceRef",bpmnSourceId);
        bpmnMap.put("targetRef",bpmnTargetId);
        return bpmnMap;
    }

    @Override
    public String getTaskType() {
        return type;
    }
}
